<template>
	<audio id="backgroudSound" ref="refAudio" style="display:none;" :preload="preload" :loop="loop" :src="music_path">
		<!-- <source :src="music_path" /> -->
	</audio>
</template>
<script>
export default {
	name: 'AudioPlay',
	data() {
		return {
			// 音频文件路径
			// music_path: require('/music/7.mp3'),
			music_path: '',
			// 是否播放音频
			is_play: false,
			preload: true,
			loop: true,
		}
	},
	methods: {
		playAudio(src) {
			this.$refs.refAudio.volume = 0.4;
			if (src) {
				this.$refs.refAudio.src = src;
			}
			if (this.is_play) {
				this.$refs.refAudio.pause();
				this.is_play = false;
			} else {
				this.$refs.refAudio.play();
				this.is_play = true;
			}
		},
	},
	mounted: function () {
		var _this = this;
		// this.$nextTick(function () {
		// 	// 仅在整个视图都被渲染之后才会运行的代码
		// 	_this.playAudio(_this.music_path);
		// });
		document.addEventListener("WeixinJSBridgeReady", function () {
			_this.is_play && _this.$refs.refAudio.play();
		}, false);
		document.addEventListener("YixinJSBridgeReady", function () {
			_this.is_play && _this.$refs.refAudio.play();
		}, false)
	}
}
</script>